package com;

import com.service.HelloService;
import io.grpc.Server;
import io.grpc.ServerBuilder;

import java.io.IOException;

/**
 * @Author onlyone
 * @create 2021/1/19
 */
public class ServerStartV2 {

    public static void main(String[] args) throws IOException, InterruptedException {

        Server server = ServerBuilder.forPort(9092)
                .addService(new HelloService())
                .build()
                .start();
        System.out.println("Starting server...");


        // 增加一个钩子，当JVM进程退出时，Server 关闭
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                server.shutdown();
                System.err.println("*** server shut down！！！！");
            }
        });

        server.awaitTermination();
    }
}
